<?php

if(!defined("N"))
{
  define("N","\n");
}

/**
 * This class is intended to be used to add stylesheets and javascript files dependently on a displayed site. For example,
 * if you want to load registration form formating stylesheet, you load in only in case user is at the
 * registration site.
 */
class LinkLoader //extends Singleton
{
  private $stylesheets = array();
  private $javascripts = array();
  private $rssfeeds = array();
  
  /*
   public static function & factory() { //implemented as
      return parent::getSingleton(__CLASS__); //calls Singleton::singleton method
   }
   */
   /* resets the list of files to include
    * @return the class instance
    */
   public function reset(){
   	  $this->stylesheets = array();
      $this->javascripts = array();
      $this->rssfeeds = array();
      
      return $this;
   }
   
   /**
    * Loads style to be displayed later by smarty in header template.
    */
   function loadCSS($name,$path="css/") {
      if(is_array($name)){
      	foreach($name as $style)
          $this->stylesheets[$path.$style]=true;
      }  	
      else
        $this->stylesheets[$path.$name]=true;
   }
   
   /**
    * Outputs loaded stylesheets.
    */
   function displayCSS() {
      $tmp="";
      foreach($this->stylesheets as $stylePath=>$v) {
         $tmp.='<link rel="stylesheet" type="text/css" href="'.$stylePath.'" />'.N;
      };
      return $tmp;
   }
   
   /**
    * Loads javascript to be displayed later by smarty in header template.
    */
   function loadJs($name,$path="js/") {
   	 if(is_array($name)){
        foreach($name as $js)
          $this->javascripts[$path.$js]=true;
      }   
      else
      $this->javascripts[$path.$name]=true;
   }

   /**
    * Outputs loaded javascript links.
    */
   function displayJs() {
      $tmp="";
      foreach($this->javascripts as $stylePath=>$v) {
         $tmp.='<script type="text/javascript" src="'.$stylePath.'"></script>'.N;
      };
      return $tmp;
   }
   
   function loadRSS($name,$titles='',$path="./"){
   	 if(is_array($name)){
        for($i = 0; $i < sizeof($name); $i++)
          $this->rssfeeds[$path.$name[$i]]= $titles[$i]?$titles[$i]:'';
      }   
      else
      $this->rssfeeds[$path.$name]=$titles;
   }
   
  function displayRSS() {
      $tmp="";
      foreach($this->rssfeeds as $rssPath=>$title) {
         $tmp.='<link rel="alternate" type="application/rss+xml" title="'.$title.'" href="'.$rssPath.'"/>'.N;
      };
      return $tmp;
   }
}
?>
